package com.stn.zjx.givingtreasure.wxapi;

/**
 * Created by 朱俊鑫 on 2018
 * 微信登录分享回调
 * @author 朱俊鑫
 * @since 2018-3
 */

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.orhanobut.logger.Logger;
import com.stn.zjx.givingtreasure.application.StnApp;
import com.stn.zjx.givingtreasure.constants.StnConstants;
import com.stn.zjx.givingtreasure.event.GoLoginEvent;
import com.stn.zjx.givingtreasure.net.impl.BaseNetImpl;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;


/** 微信客户端回调activity示例 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    // IWXAPI 是第三方app和微信通信的openapi接口
    private IWXAPI api;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        api = WXAPIFactory.createWXAPI(this, StnConstants.wx_key, false);
        api.handleIntent(getIntent(), this);
        super.onCreate(savedInstanceState);
    }
    @Override
    public void onReq(BaseReq arg0) { }

    @Override
    public void onResp(BaseResp resp) {
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                    if (resp instanceof SendAuth.Resp) {
                    //登录类型
                    SendAuth.Resp sResp = (SendAuth.Resp) resp;

                 //   EventBus.getDefault().post(sResp);

                    BaseNetImpl.wx_getToken(sResp.code, StnConstants.wx_key, StnConstants.wx_secret, "wx_getToken", new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            try {
                                JSONObject data=new JSONObject(response.body());
                                String access_token = data.getString("access_token"); //接口调用凭证
                                String openid = data.getString("openid"); //授权用户唯一标识
                                BaseNetImpl.wx_getInfo(access_token, openid, "wx_getInfo", new StringCallback() {
                                    @Override
                                    public void onSuccess(Response<String> response) {
                                        try {
                                            JSONObject data=new JSONObject(response.body());
                                            String nickname=data.getString("nickname");
                                            String sex=data.getString("sex");
                                            String headimgurl=data.getString("headimgurl");
                                            GoLoginEvent event=new GoLoginEvent();
                                             event.loginType=1;
                                             if(sex.equals("1")){
                                                 event.sex="男";
                                             }else{
                                                 event.sex="女";
                                             }
                                              event.userAvatar=headimgurl;
                                             event.userNickname=nickname;
                                             event.id=openid;
                                            EventBus.getDefault()
                                                    .post(event);
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }

                                    }
                                });
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });
                } else {
                    //分享类型
//                    Toast.makeText(getApplicationContext(),"分享成功", Toast.LENGTH_SHORT).show();

//                    分享成功的回调
              //      EventBus.getDefault().post(Type.SHARE);
                }
                //分享成功
                break;
            //取消操作
            case BaseResp.ErrCode.ERR_USER_CANCEL:
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
            //    Toast.makeText(getApplicationContext(), "取消操作", Toast.LENGTH_SHORT).show();
                //EventBus.getDefault().post(Type.FAIL);
                break;
            //调用操作失败
            default:
          //      Toast.makeText(getApplicationContext(), "操作失败", Toast.LENGTH_SHORT).show();

                break;
        }
        finish();
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

}